<?php
namespace PhpBoot\DB\rules\delete;
use PhpBoot\DB\impls\JoinImpl;
use PhpBoot\DB\impls\JoinOnImpl;
use PhpBoot\DB\rules\basic\BasicRule;
use PhpBoot\DB\impls\DeleteImpl;
use PhpBoot\DB\rules\basic\WhereRule;

require_once dirname(__DIR__).'/impls.php';
require_once __DIR__.'/basic.php';

class DeleteRule extends BasicRule
{
    /**
     * deleteFrom('table') => "DELETE table,subTable FROM table"
     * @param string $table
     * @return JoinRule
     */
    public function deleteFrom($table, $subTable='') {
        DeleteImpl::deleteFrom($this->context, $table, $subTable);
        return new JoinRule($this->context);
    }
}

class JoinRule extends WhereRule
{
    /**
     * join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id"
     * @param string $table
     * @return JoinOnRule
     */
    public function join($table){
        JoinImpl::join($this->context,null, $table);
        return new JoinOnRule($this->context);
    }
    /**
     * leftJoin('table1')->on('table0.id=table1.id') => "LEFT JOIN table1 ON table0.id=table1.id"
     * @param string $table
     * @return JoinOnRule
     */
    public function leftJoin($table){
        JoinImpl::join($this->context,'LEFT', $table);
        return new JoinOnRule($this->context);
    }
    /**
     * rightJoin('table1')->on('table0.id=table1.id') => "RIGHT JOIN table1 ON table0.id=table1.id"
     * @param string $table
     * @return JoinOnRule
     */
    public function rightJoin($table) {
        JoinImpl::join($this->context,'RIGHT', $table);
        return new JoinOnRule($this->context);
    }
    /**
     * innerJoin('table1')->on('table0.id=table1.id') => "INNER JOIN table1 ON table0.id=table1.id"
     * @param string $table
     * @return JoinOnRule
     */
    public function innerJoin($table) {
        JoinImpl::join($this->context,'INNER', $table);
        return new JoinOnRule($this->context);
    }
}

class JoinOnRule extends BasicRule
{
    /**
     * join('table1')->on('table0.id=table1.id') => "JOIN table1 ON table0.id=table1.id"
     * @param string $condition
     * @return JoinRule
     */
    public function on($condition){
        JoinOnImpl::on($this->context, $condition);
        return new JoinRule($this->context);
    }
}